﻿Imports System.Data

Partial Class WebForm_ManageRole_ManageRole
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            ObjectDataSource_Role.SelectParameters("PapaID").DefaultValue = DirectCast(FormView1.FindControl("ddlParent"), DropDownList).SelectedValue

            Me.BindDivision(TryCast(Me.FormView1.FindControl("ddlDivision"), DropDownList))
        End If
    End Sub

    Private Sub BindDivision(ByVal ddlControl As DropDownList)
        ddlControl.DataTextField = "Text"
        ddlControl.DataValueField = "Value"
        ddlControl.DataSource = MyObj.GetCodeMappingData("Product", "Division")
        ddlControl.DataBind()

        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    End Sub

    Protected Sub ddlRolePapas_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        TGridView1_DataBind()
    End Sub

    'TGridView1 執行DataBind
    Protected Sub TGridView1_DataBind()
        ObjectDataSource_Role.SelectParameters("PapaID").DefaultValue = DirectCast(Me.FormView1.FindControl("ddlParent"), DropDownList).SelectedValue
        TGridView1.DataBind()
    End Sub

    'TGridView1 RowUpdated後要更新ddlParent
    Protected Sub TGridView1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
        DirectCast(FormView1.FindControl("ddlParent"), DropDownList).DataBind()
    End Sub

    'TGridView1 RowDataBound
    Protected Sub TGridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

            If drv("Parent").ToString() = DirectCast(FormView1.FindControl("ddlParent"), DropDownList).SelectedValue Then
                e.Row.Cells(3).Font.Bold = True
            End If

            If TryCast(e.Row.FindControl("ddlDivision"), DropDownList) IsNot Nothing Then
                Me.BindDivision(TryCast(e.Row.FindControl("ddlDivision"), DropDownList))
            End If
        End If
    End Sub

    '編輯-RowUpdating
    Protected Sub TGridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        Dim ddlDivision As DropDownList = DirectCast(Me.TGridView1.Rows(e.RowIndex).FindControl("ddlDivision"), DropDownList)
        e.NewValues("Division") = ddlDivision.SelectedValue

        e.NewValues("Active") = IIf(DirectCast(TGridView1.Rows(e.RowIndex).FindControl("chkActive"), CheckBox).Checked = True, "Y", "N")
    End Sub

    '新增
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlParent As DropDownList = DirectCast(Me.FormView1.FindControl("ddlParent"), DropDownList)
        Dim ddlDivision As DropDownList = DirectCast(Me.FormView1.FindControl("ddlDivision"), DropDownList)
        Dim txtRoleName As TextBox = DirectCast(Me.FormView1.FindControl("txtRoleName"), TextBox)
        Dim txtRoleDescription As TextBox = DirectCast(Me.FormView1.FindControl("txtRoleDescription"), TextBox)

        ObjectDataSource_Role.InsertParameters("Parent").DefaultValue = ddlParent.SelectedValue
        ObjectDataSource_Role.InsertParameters("RoleName").DefaultValue = txtRoleName.Text
        ObjectDataSource_Role.InsertParameters("RoleDescription").DefaultValue = txtRoleDescription.Text
        ObjectDataSource_Role.InsertParameters("Division").DefaultValue = ddlDivision.SelectedValue.ToString()

        Try
            ObjectDataSource_Role.Insert()

            ddlParent.SelectedIndex = -1
            txtRoleName.Text = String.Empty
            txtRoleDescription.Text = String.Empty
        Catch ex As Exception

            If DirectCast(ex.InnerException, System.Data.SqlClient.SqlException).Number = 2627 Then
                CommonLib2.clsCommon.ShowMessage(True, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, "The role name already exists! Use other names!")
            Else
                CommonLib2.clsCommon.ShowMessage(True, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, ex.InnerException.Message.Replace("'", "\'"))

            End If
        End Try

    End Sub

    '編輯-ObjectDataSource_Role_Updated
    Protected Sub ObjectDataSource_Role_Updated(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs)

        If e.Exception IsNot Nothing AndAlso e.Exception.InnerException IsNot Nothing Then
            If DirectCast(e.Exception.InnerException, System.Data.SqlClient.SqlException).Number = 2627 Then
                CommonLib2.clsCommon.ShowMessage(True, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, "The role name already exists! Use other names!")
            Else
                CommonLib2.clsCommon.ShowMessage(True, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, e.Exception.InnerException.Message.Replace("'", "\'"))

            End If
        End If

    End Sub

    '開窗設定Brand權限
    Protected Sub btnProduct_Click(ByVal sender As Object, ByVal e As EventArgs)
        TGridView1.EditIndex = -1
        OpenManageRoleBrandMapping1.RoleID = CInt(TGridView1.DataKeys(DirectCast(DirectCast(sender, Button).NamingContainer, GridViewRow).RowIndex)(0))
        OpenManageRoleBrandMapping1.InitDataAndShow()

    End Sub

    '開窗設定Cnannel權限
    Protected Sub btnChannel_Click(ByVal sender As Object, ByVal e As EventArgs)
        TGridView1.EditIndex = -1
        OpenManageRoleChannelMapping1.RoleID = CInt(TGridView1.DataKeys(DirectCast(DirectCast(sender, Button).NamingContainer, GridViewRow).RowIndex)(0))
        OpenManageRoleChannelMapping1.InitData()
    End Sub

    '開窗設定Function權限
    Protected Sub btnFunction_Click(ByVal sender As Object, ByVal e As EventArgs)
        TGridView1.EditIndex = -1
        OpenManageRoleProgramMapping1.RoleID = CInt(TGridView1.DataKeys(DirectCast(DirectCast(sender, Button).NamingContainer, GridViewRow).RowIndex)(0))
        OpenManageRoleProgramMapping1.InitDataAndShow()
    End Sub

    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "Y", True, False))
    End Function

End Class
